﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;
using System.Linq.Expressions;
using System.Web.Security;

namespace BitPortal.Business
{
    public static partial class PageBLL
    {
        /// <summary>
        /// Lay duong link da Rewrite
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>

        public static string GetSEOUrl(this PortalPage page)
        {
            return "/" + page.Name.ToSeoString() + "-pg" + page.PageID + ".aspx";
        }

        public static IQueryable<PortalPage> GetAll()
        {
            return (new ExtendedBitPortalEntities()).PortalPages.Where(ent => ent.IsDeleted == false);
        }

        public static int Insert(this PortalPage page)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            bitEnt.PortalPages.AddObject(page);
            return bitEnt.SaveChanges();
        }

        public static PortalPage GetByID(long pageID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.PortalPages.SingleOrDefault(ent => ent.PageID == pageID);
        }

        public static PortalPage GetClassName(string className)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.PortalPages.SingleOrDefault(ent => ent.ClassName == className);
        }

        public static void DeletePermanently(PortalPage page, ExtendedBitPortalEntities bitEnt)
        {
            foreach (PortalPage child in bitEnt.PortalPages.Where(ent => ent.ParentID == page.PageID))
            {
                DeletePermanently(child, bitEnt);
            }
            if (page.IsBasePage && !Roles.IsUserInRole("RootAdmin")) throw new Exception("Không thể xóa trang mặc định của hệ thống : '" + page.Name + "'");
            bitEnt.PortalPages.DeleteObject(page);
        }
        /// <summary>
        /// Xoa truc tiep
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static void DeletePermanently(int pageID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            PortalPage page = bitEnt.PortalPages.SingleOrDefault(ac => ac.PageID == pageID);
            if (page != null)
            {
                DeletePermanently(page, bitEnt);
            }
            bitEnt.SaveChanges();
        }

        public static void Delete(PortalPage page, ExtendedBitPortalEntities bitEnt)
        {
            foreach (PortalPage child in bitEnt.PortalPages.Where(ent => ent.ParentID == page.PageID))
            {
                Delete(child, bitEnt);
            } 
            if (page.IsBasePage && !Roles.IsUserInRole("RootAdmin")) throw new Exception("Không thể xóa trang mặc định của hệ thống : '" + page.Name + "'");
            page.IsDeleted = true;
        }
        /// <summary>
        /// Xoa tam thoi(dat IsDeleted = true)
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static void Delete(int pageID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            PortalPage category = bitEnt.PortalPages.SingleOrDefault(ac => ac.PageID == pageID);
            if (category != null)
            {
                Delete(category, bitEnt);
            }
            bitEnt.SaveChanges();
        }


        /// <summary>
        /// Lay ra danh sach tat ca nhom con chau cua nhom hien tai.
        /// </summary>
        /// <param name="parentArtCate"></param>
        /// <param name="childs"></param>
        public static void GetAllChilds(this PortalPage page, List<PortalPage> childs)
        {
            if (page == null)
            {
                foreach (PortalPage ac in GetAll().Where(ent => ent.ParentID == null).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }
            else
            {
                foreach (PortalPage ac in GetAll().Where(ent => ent.ParentID == page.PageID).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }

        }

        /// <summary>
        /// Lay danh sach nhung trang cha cua trang hien tai.
        /// </summary>
        /// <param name="childArtCate"></param>
        /// <returns></returns>
        public static List<PortalPage> GetParents(this PortalPage page)
        {
            List<PortalPage> listParents = new List<PortalPage>();
            while (page.ParentID != null)
            {
                page = GetAll().SingleOrDefault(ent => ent.PageID == page.ParentID);
                if (page == null)
                {
                    break;
                }
                listParents.Add(page);
            }
            return listParents;
        }

        /// <summary>
        /// Lay ra danh sach trang con cua trang hien tai.
        /// </summary>
        /// <param name="parentArtCate"></param>
        /// <returns></returns>
        public static List<PortalPage> GetChilds(this PortalPage page)
        {
            if (page == null)
            {
                return GetAll().Where(ent => ent.ParentID == null).ToList();
            }
            return GetAll().Where(ent => ent.ParentID == page.PageID).ToList();
        }

        public static List<PortalPage> GetByCondition(string languageCode = null, bool? isEnabled = null, int? parentID = -1, int start = -1, int limit = -1, string sort = null, string dir = null)
        {
            //ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = GetAll().Where(ent => (languageCode == null || ent.LanguageCode.Equals(languageCode)) &&
                (isEnabled == null || ent.IsEnabled == isEnabled) &&
                (parentID == -1 || ent.ParentID == parentID)
                );

            //Start Sorting
            if (sort != null)
            {
                var param = Expression.Parameter(typeof(PortalPage), "ac");
                var sortExpression = Expression.Lambda<Func<PortalPage, object>>(Expression.Property(param, sort), param);
                if (dir == "DESC")
                {
                    list = list.OrderByDescending(sortExpression);
                }
                else
                {
                    list = list.OrderBy(sortExpression);
                }
            }
            //Start Paging
            if (start >= 0 && limit > 0)
            {
                list = list.Skip(start).Take(limit);
            }

            return list.ToList();
        }

        public static List<Module> GetModule(this PortalPage page, bool? isEnabled = null, bool onAllPage = true)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            if (!onAllPage)
            {
                return (from p in page.Modules
                        orderby p.SortOrder descending, p.ModuleID descending
                        select p).Where(ent => isEnabled == null || ent.IsEnabled == isEnabled).ToList();
            }
            return (from p in bitEnt.Modules
                    where (p.OnAllPage || p.PageID == page.PageID) && (isEnabled == null || p.IsEnabled == isEnabled)
                    orderby p.SortOrder descending, p.ModuleID descending
                    select p).ToList();
        }

    }
}